<?php
namespace app\property\controller;
use think\Loader;
class RecordList extends Base {
    function index() {
        //分配日期
        $date = input('date')?input('date'):date("Y/m/d");
        $this->assign("date",$date);
        //dump(input());
        $where = ['date'=>$date,'userid'=>getSession("user","property_")['userid']];
        //派工单       
        $workrecords = db('workrecord')->where($where)->select();
        $this->assign("workrecords",$workrecords);
        //待工单
        $restrecords = db("restrecord")->where($where)->select();
        $this->assign("restrecords",$restrecords);
       
        return $this->fetch();
    }
    
    public function edit_order(){
        //分配日期
        $date = input('date')?input('date'):date("Y/m/d");
        $this->assign("date",$date);
        //获取工种
        $worktypes = db('worktype')->field("id as value,worktype as title")->select();
        $this->assign("worktypes",json_encode($worktypes));
        $workcontents = db("workcontent")->field("workcontent as title,format(coefficient,2) as value,typeid")->select();
        $res = [];
        foreach ($worktypes as $worktype)
        {
            foreach ($workcontents as $workcontent)
            {
                if ($worktype['value']==$workcontent['typeid']){
                    if (!isset($res[$worktype['value']])){
                        $res[$worktype['value']] = [];
                    }
                    unset($workcontent['typeid']);
                    array_push($res[$worktype['value']],$workcontent);
                }
            }
        }
        $this->assign("workcontents",json_encode($res));
        
        //
        $type = input('type');
        $id = input('id');
        if ($type=='work'){
            $workrecord = db("workrecord")->where(['id'=>$id])->find();
            $this->assign("workrecord",$workrecord);
        }else {
            $restrecord = db("restrecord")->where(['id'=>$id])->find();
            $this->assign("restrecord",$restrecord);
        }
        
       return $this->fetch();
    }
    
    public function save_work(){
        try {
            $data = input();
            $data['update_time'] = time();
            $data['userid'] = getSession("user","property_")['userid'];
            $data['username'] = getSession("user","property_")['name'];
            //初始化一个验证器
            $validate = Loader::validate('Workrecord');
            if (!$validate->check($data)){
                //生成新令牌
                $hash = request()->token("hash");
                echo "{\"errcode\":-1003,\"errmsg\":\"".$validate->getError()."\",\"__hash__\":\"".$hash."\"}";
                return;
            }
        
            //添加数据
            $res=db("workrecord")->update($data);
            if ($res){
                echo "{\"errcode\":0,\"errmsg\":\"派工单修改成功！\"}";
                return;
            }else{
                echo "{\"errcode\":-1001,\"errmsg\":\"派工单修改失败!\"}";
                return;
            }
        } catch (\Exception $e) {
            echo "{\"errcode\":-1002,\"errmsg\":\"".$e->getMessage()."\"}";
            return;
        }
    }
    
    public function save_rest(){
        try {
            $data = input();
            $data['update_time'] = time();
            $data['userid'] = getSession("user","property_")['userid'];
            $data['username'] = getSession("user","property_")['name'];
            //初始化一个验证器
            $validate = Loader::validate('Restrecord');
            if (!$validate->check($data)){
                //生成新令牌
                $hash = request()->token("hash");
                echo "{\"errcode\":-1003,\"errmsg\":\"".$validate->getError()."\",\"__hash__\":\"".$hash."\"}";
                return;
            }
    
            //添加数据
            $res=db("restrecord")->update($data);
            if ($res){
                echo "{\"errcode\":0,\"errmsg\":\"待工单修改成功！\"}";
                return;
            }else{
                echo "{\"errcode\":-1001,\"errmsg\":\"待工单修改失败!\"}";
                return;
            }
        } catch (\Exception $e) {
            echo "{\"errcode\":-1002,\"errmsg\":\"".$e->getMessage()."\"}";
            return;
        }
    }
    
    
    public function del_work(){
        $id = input('id');
        try {
            $res = db("workrecord")->where("id",$id)->delete();
            if ($res){
                echo "{\"errcode\":\"0\",\"errmsg\":\"删除成功！\"}";
            }else{
                echo "{\"errcode\":\"-1001\",\"errmsg\":\"删除失败！\"}";
            }
        } catch (\Exception $e) {
            echo "{\"errcode\":\"-1002\",\"errmsg\":\"".$e->getMessage()."\"}";
        }
    }
    
    public function del_rest(){
        $id = input('id');
        try {
            $res = db("restrecord")->where("id",$id)->delete();
            if ($res){
                echo "{\"errcode\":\"0\",\"errmsg\":\"删除成功！\"}";
            }else{
                echo "{\"errcode\":\"-1001\",\"errmsg\":\"删除失败！\"}";
            }
        } catch (\Exception $e) {
            echo "{\"errcode\":\"-1002\",\"errmsg\":\"".$e->getMessage()."\"}";
        }
    }
}